package zjw.springboot2.test.controller;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;
import java.util.Objects;

/**
 * Created by zhoujiawei on 2020-12-22.
 */
@RequestMapping("/httpClient")
@Controller
@Slf4j
public class HttpClientController {
    @PostMapping("test")
    @ResponseBody
    public HttpResponse test(@RequestBody String params) {
        Map<String, String> map = JSON.parseObject(params, Map.class);
        YundaoClient client = new YundaoClient(map.get("subscribeUrl"), map.get("xml"));
        HttpResponse result = client.connect();
        if (!Objects.equals(result.getCode(), HttpStatus.OK)) {
            return result;
        }
        String requestParam = String.format("partnerid=%s&mailno=%s&type=json", map.get("partnerid")
                , map.get("mailNo"));
        client = new YundaoClient(map.get("routeUrl"), requestParam);
        result = client.connect();
        return result;
    }
}
